package com.treasure.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treasure.common.model.SessionUser;
import com.treasure.common.model.SessionUserHolder;
import com.treasure.common.types.Corp;
import com.treasure.common.utils.RandomIdGenerator;
import com.treasure.infrastructure.domain.impl.client.EmailSenderClient;
import com.treasure.infrastructure.model.po.CorpEmailAccount;
import com.treasure.infrastructure.repository.dao.mapper.CorpEmailAccountMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treasure.web.command.request.CorpEmailAccountEditRequest;
import com.treasure.web.command.request.CorpEmailTestRequest;
import com.treasure.web.command.response.CorpEmailAccountResponse;
import com.treasure.web.mapstruct.CorpEmailAccountAssembler;
import com.treasure.web.service.ICorpEmailAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sunhongmin
 * @since 2023-11-17
 */
@Service
public class CorpEmailAccountServiceImpl extends ServiceImpl<CorpEmailAccountMapper, CorpEmailAccount> implements ICorpEmailAccountService {

    @Resource
    private EmailSenderClient emailSenderClient;

    @Override
    public CorpEmailAccountResponse getEmailAccount(Corp corp) {
        CorpEmailAccount corpEmailAccount = getCorpEmailAccountPO(corp);
        return CorpEmailAccountAssembler.INSTANCE.convertToCorpEmailAccountResponse(corpEmailAccount);
    }

    private CorpEmailAccount getCorpEmailAccountPO(Corp corp) {
        QueryWrapper<CorpEmailAccount> emailAccountQuery = new QueryWrapper<>();
        emailAccountQuery.lambda().eq(CorpEmailAccount::getCorpId, corp.getValue());
        return getOne(emailAccountQuery);
    }

    @Override
    public void editEmailAccount(Corp corp, CorpEmailAccountEditRequest corpEmailAccountEditRequest) {
        CorpEmailAccount corpEmailAccountPO = getCorpEmailAccountPO(corp);
        if (Objects.nonNull(corpEmailAccountPO)) {
            CorpEmailAccountAssembler.INSTANCE.updateEmailAccount(corpEmailAccountPO, corpEmailAccountEditRequest);
            if (StringUtils.isNotBlank(corpEmailAccountEditRequest.getePassword())) {
                corpEmailAccountPO.setePassword(corpEmailAccountEditRequest.getePassword());
            }
            updateById(corpEmailAccountPO);
        } else {
            CorpEmailAccount corpEmailAccount = new CorpEmailAccount();
            CorpEmailAccountAssembler.INSTANCE.updateEmailAccount(corpEmailAccount, corpEmailAccountEditRequest);
            if (StringUtils.isNotBlank(corpEmailAccountEditRequest.getePassword())) {
                corpEmailAccount.setePassword(corpEmailAccountEditRequest.getePassword());
            }
            corpEmailAccount.setPort(-1);// 保留参数，需要时赋值
            corpEmailAccount.setCorpId(corp.getValue());
            corpEmailAccount.setDeleteFlag(0);
            corpEmailAccount.setCreateTime(LocalDateTime.now());
            save(corpEmailAccount);
        }
    }

    @Override
    public String corpEmailTest(CorpEmailTestRequest corpEmailTestRequest) {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        String traceId = RandomIdGenerator.traceId();
        emailSenderClient.sendTextMailMessage(new Corp(sessionUser.getCorpId()), Collections.singletonList(corpEmailTestRequest.getReceiver()), "【易智】测试邮件", "当您收到此邮件，证明后台配置邮箱成功！Verify code：" + traceId);
        return traceId;
    }


}
